//tc is healthy, just do it
#include <bits/stdc++.h>
using namespace std;

class PowerOutage
{
private:
	int Deep[60], Size;
	void Dfs(int u, int fa, vector<int> fromJunction, vector<int> toJunction, vector<int> ductLength);

public:
	int estimateTimeOut(vector<int> fromJunction, vector<int> toJunction, vector<int> ductLength);
};
int PowerOutage::estimateTimeOut(vector<int> fromJunction, vector<int> toJunction, vector<int> ductLength)
{
	Size = fromJunction.size();
	Deep[0] = 0;
	Dfs(0, 0, fromJunction, toJunction, ductLength);
	int Ans = 0, Max = 0;
	for (int i = 0; i < Size; ++i)
		Ans += ductLength[i] * 2;
	for (int i = 0; i < 50; ++i)
		Max = max(Max, Deep[i]);
	return Ans - Max;
}
void PowerOutage::Dfs(int u, int fa, vector<int> fromJunction, vector<int> toJunction, vector<int> ductLength)
{
	for (int i = 0; i < Size; ++i)
		if (fromJunction[i] == u && toJunction[i] != fa)
		{
			Deep[toJunction[i]] = Deep[u] + ductLength[i];
			Dfs(toJunction[i], u, fromJunction, toJunction, ductLength);
		}
	for (int i = 0; i < Size; ++i)
		if (toJunction[i] == u && fromJunction[i] != fa)
		{
			Deep[fromJunction[i]] = Deep[u] + ductLength[i];
			Dfs(fromJunction[i], u, fromJunction, toJunction, ductLength);
		}
	return;
}

// BEGIN CUT HERE
namespace moj_harness
{
int run_test_case(int);
void run_test(int casenum = -1, bool quiet = false)
{
	if (casenum != -1)
	{
		if (run_test_case(casenum) == -1 && !quiet)
			cerr << "Illegal input! Test case " << casenum << " does not exist." << endl;
		return;
	}

	int correct = 0, total = 0;
	for (int i = 0;; ++i)
	{
		int x = run_test_case(i);
		if (x == -1)
		{
			if (i >= 100)
				break;
			continue;
		}
		correct += x;
		++total;
	}

	if (total == 0)
	{
		cerr << "No test cases run." << endl;
	}
	else if (correct < total)
	{
		cerr << "Some cases FAILED (passed " << correct << " of " << total << ")." << endl;
	}
	else
	{
		cerr << "All " << total << " tests passed!" << endl;
	}
}

int verify_case(int casenum, const int &expected, const int &received, clock_t elapsed)
{
	cerr << "Example " << casenum << "... ";

	string verdict;
	vector<string> info;
	char buf[100];

	if (elapsed > CLOCKS_PER_SEC / 200)
	{
		sprintf(buf, "time %.2fs", elapsed * (1.0 / CLOCKS_PER_SEC));
		info.push_back(buf);
	}

	if (expected == received)
	{
		verdict = "PASSED";
	}
	else
	{
		verdict = "FAILED";
	}

	cerr << verdict;
	if (!info.empty())
	{
		cerr << " (";
		for (int i = 0; i < (int)info.size(); ++i)
		{
			if (i > 0)
				cerr << ", ";
			cerr << info[i];
		}
		cerr << ")";
	}
	cerr << endl;

	if (verdict == "FAILED")
	{
		cerr << "    Expected: " << expected << endl;
		cerr << "    Received: " << received << endl;
	}

	return verdict == "PASSED";
}

int run_test_case(int casenum)
{
	switch (casenum)
	{
	case 0:
	{
		int fromJunction[] = {0};
		int toJunction[] = {1};
		int ductLength[] = {10};
		int expected__ = 10;

		clock_t start__ = clock();
		int received__ = PowerOutage().estimateTimeOut(vector<int>(fromJunction, fromJunction + (sizeof fromJunction / sizeof fromJunction[0])), vector<int>(toJunction, toJunction + (sizeof toJunction / sizeof toJunction[0])), vector<int>(ductLength, ductLength + (sizeof ductLength / sizeof ductLength[0])));
		return verify_case(casenum, expected__, received__, clock() - start__);
	}
	case 1:
	{
		int fromJunction[] = {0, 1, 0};
		int toJunction[] = {1, 2, 3};
		int ductLength[] = {10, 10, 10};
		int expected__ = 40;

		clock_t start__ = clock();
		int received__ = PowerOutage().estimateTimeOut(vector<int>(fromJunction, fromJunction + (sizeof fromJunction / sizeof fromJunction[0])), vector<int>(toJunction, toJunction + (sizeof toJunction / sizeof toJunction[0])), vector<int>(ductLength, ductLength + (sizeof ductLength / sizeof ductLength[0])));
		return verify_case(casenum, expected__, received__, clock() - start__);
	}
	case 2:
	{
		int fromJunction[] = {0, 0, 0, 1, 4};
		int toJunction[] = {1, 3, 4, 2, 5};
		int ductLength[] = {10, 10, 100, 10, 5};
		int expected__ = 165;

		clock_t start__ = clock();
		int received__ = PowerOutage().estimateTimeOut(vector<int>(fromJunction, fromJunction + (sizeof fromJunction / sizeof fromJunction[0])), vector<int>(toJunction, toJunction + (sizeof toJunction / sizeof toJunction[0])), vector<int>(ductLength, ductLength + (sizeof ductLength / sizeof ductLength[0])));
		return verify_case(casenum, expected__, received__, clock() - start__);
	}
	case 3:
	{
		int fromJunction[] = {0, 0, 0, 1, 4, 4, 6, 7, 7, 7, 20};
		int toJunction[] = {1, 3, 4, 2, 5, 6, 7, 20, 9, 10, 31};
		int ductLength[] = {10, 10, 100, 10, 5, 1, 1, 100, 1, 1, 5};
		int expected__ = 281;

		clock_t start__ = clock();
		int received__ = PowerOutage().estimateTimeOut(vector<int>(fromJunction, fromJunction + (sizeof fromJunction / sizeof fromJunction[0])), vector<int>(toJunction, toJunction + (sizeof toJunction / sizeof toJunction[0])), vector<int>(ductLength, ductLength + (sizeof ductLength / sizeof ductLength[0])));
		return verify_case(casenum, expected__, received__, clock() - start__);
	}
	case 4:
	{
		int fromJunction[] = {0, 0, 0, 0, 0};
		int toJunction[] = {1, 2, 3, 4, 5};
		int ductLength[] = {100, 200, 300, 400, 500};
		int expected__ = 2500;

		clock_t start__ = clock();
		int received__ = PowerOutage().estimateTimeOut(vector<int>(fromJunction, fromJunction + (sizeof fromJunction / sizeof fromJunction[0])), vector<int>(toJunction, toJunction + (sizeof toJunction / sizeof toJunction[0])), vector<int>(ductLength, ductLength + (sizeof ductLength / sizeof ductLength[0])));
		return verify_case(casenum, expected__, received__, clock() - start__);
	}

		// custom cases

		/*      case 5: {
			int fromJunction[]        = ;
			int toJunction[]          = ;
			int ductLength[]          = ;
			int expected__            = ;

			clock_t start__           = clock();
			int received__            = PowerOutage().estimateTimeOut( vector <int>( fromJunction, fromJunction + ( sizeof fromJunction / sizeof fromJunction[0] ) ), vector <int>( toJunction, toJunction + ( sizeof toJunction / sizeof toJunction[0] ) ), vector <int>( ductLength, ductLength + ( sizeof ductLength / sizeof ductLength[0] ) ) );
			return verify_case( casenum, expected__, received__, clock()-start__ );
		}*/
		/*      case 6: {
			int fromJunction[]        = ;
			int toJunction[]          = ;
			int ductLength[]          = ;
			int expected__            = ;

			clock_t start__           = clock();
			int received__            = PowerOutage().estimateTimeOut( vector <int>( fromJunction, fromJunction + ( sizeof fromJunction / sizeof fromJunction[0] ) ), vector <int>( toJunction, toJunction + ( sizeof toJunction / sizeof toJunction[0] ) ), vector <int>( ductLength, ductLength + ( sizeof ductLength / sizeof ductLength[0] ) ) );
			return verify_case( casenum, expected__, received__, clock()-start__ );
		}*/
		/*      case 7: {
			int fromJunction[]        = ;
			int toJunction[]          = ;
			int ductLength[]          = ;
			int expected__            = ;

			clock_t start__           = clock();
			int received__            = PowerOutage().estimateTimeOut( vector <int>( fromJunction, fromJunction + ( sizeof fromJunction / sizeof fromJunction[0] ) ), vector <int>( toJunction, toJunction + ( sizeof toJunction / sizeof toJunction[0] ) ), vector <int>( ductLength, ductLength + ( sizeof ductLength / sizeof ductLength[0] ) ) );
			return verify_case( casenum, expected__, received__, clock()-start__ );
		}*/
	default:
		return -1;
	}
}
} // namespace moj_harness

int main(int argc, char *argv[])
{
	if (argc == 1)
	{
		moj_harness::run_test();
	}
	else
	{
		for (int i = 1; i < argc; ++i)
			moj_harness::run_test(atoi(argv[i]));
	}
}
// END CUT HERE
